// pages/index/component/nearCar/nearCar.js
import { nlist } from '../../../api/shop';
import { list } from '../../../api/shopCategory'
const app = getApp()
import util from '../../../utils/util'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  lifetimes: {
    attached() {
      this.loadShopCategory()

    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    shopCategoryId: '',
    shopCategorys: [],
    pageNum: 1,
    tabCur: 0, //默认选中
    shops: [],
    tabs: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onReachBottom: function () {
      this.data.pageNum += 1
      this.loadShop()

    },
    downRefresh() {
      this.data.shops = []
      this.data.pageNum = 1
      this.data.shopCategoryId = ""
      this.loadShop()
    },
    loadShop() {
      nlist({
        scopeType: 4,
        pageNum: this.data.pageNum,
        shopCategoryId: this.data.shopCategoryId,
        lng: app.globalData.lng,
        lat: app.globalData.lat,
      }).then(res => {
        res.data.data.forEach(item => {
          item.logo = util.thumbnail(item.logo,190,190)
          item.distance = util.rateConverter(item.distance)
          item.score = item.score.toFixed(1)
          item.images = []
          for (var i = 0; i < item.score; i++) {
            item.images.push('https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/canyin/canyin-pingfen-red.png')
          }
          for (var k = item.images.length; k < 5; k++) {
            item.images.push('https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/canyin/canyin-pingfen-gray.png')
          }
          this.data.shops.push(item)
        })
        // this.data.shops=res.data.data
        this.setData({
          shops: this.data.shops
        })
        wx.stopPullDownRefresh()
      })
    },
    loadShopCategory() {
      list({
        scopeType: 4
      }).then(res => {
        res.data.unshift({
          name: '全部',
          id: '',
        })
        res.data.forEach((item, index) => {
          item.index = index
        })
        this.loadShop()
        this.setData({
          shopCategorys: res.data
        })
      })
    },
    tabSelect(e) {
      this.data.shops = []
      this.data.pageNum = 1
      this.setData({
        shopCategoryId: e.currentTarget.dataset.id,
        tabCur: e.currentTarget.dataset.index,
        scrollLeft: (e.currentTarget.dataset.index - 2) * 200
      })
      this.loadShop()
    }

  }
})
